{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Integers - Operations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "Addition, subtraction, multiplication and exponentiation of integers always result in an integer. (In the case of exponentiation this holds only for positive integer exponents.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(2 + 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(3 - 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(3 * 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(3 ** 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "But the standard division operator `/` **always** results in a float value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(2 / 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(10 / 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `math.floor()` method will return the floor of any number."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For non-negative values (>= 0), the floor of the value is the same as the integer portion of the value (truncation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.floor(3.15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.floor(3.9999999)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "However, this is not the case for negative values:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-4"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.floor(-3.15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-4"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.floor(-3.0000001)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### The Floor Division Operator"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The floor division operator `a//b` is the floor of `a / b`\n",
    "\n",
    "i.e. `a // b = math.floor(a / b)`\n",
    "\n",
    "This is true whether `a` and `b` are positive or negative."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0625\n",
      "2\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "a = 33\n",
    "b = 16\n",
    "print(a/b)\n",
    "print(a//b)\n",
    "print(math.floor(a/b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For positive numbers, `a//b` is basically the same as truncating (taking the integer portion) of `a / b`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "But this is **not** the case for negative numbers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-33/16 = -2.0625\n",
      "trunc(-33/16) = -2\n",
      "-33//16 = -3\n",
      "floor(-33//16) = -3\n"
     ]
    }
   ],
   "source": [
    "a = -33\n",
    "b = 16\n",
    "print('{0}/{1} = {2}'.format(a, b, a/b))\n",
    "print('trunc({0}/{1}) = {2}'.format(a, b, math.trunc(a/b)))\n",
    "print('{0}//{1} = {2}'.format(a, b, a//b))\n",
    "print('floor({0}//{1}) = {2}'.format(a, b, math.floor(a/b)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "33/-16 = -2.0625\n",
      "trunc(33/-16) = -2\n",
      "33//-16 = -3\n",
      "floor(33//-16) = -3\n"
     ]
    }
   ],
   "source": [
    "a = 33\n",
    "b = -16\n",
    "print('{0}/{1} = {2}'.format(a, b, a/b))\n",
    "print('trunc({0}/{1}) = {2}'.format(a, b, math.trunc(a/b)))\n",
    "print('{0}//{1} = {2}'.format(a, b, a//b))\n",
    "print('floor({0}//{1}) = {2}'.format(a, b, math.floor(a/b)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### The Modulo Operator"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The modulo operator and the floor division operator will always satisfy the following equation:\n",
    "\n",
    "``a = b * (a // b) + a % b``"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "13/4 = 3.25\n",
      "13//4 = 3\n",
      "13%4 = 1\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "a = 13\n",
    "b = 4\n",
    "print('{0}/{1} = {2}'.format(a, b, a/b))\n",
    "print('{0}//{1} = {2}'.format(a, b, a//b))\n",
    "print('{0}%{1} = {2}'.format(a, b, a%b))\n",
    "print(a == b * (a//b) + a%b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-13/4 = -3.25\n",
      "-13//4 = -4\n",
      "-13%4 = 3\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "a = -13\n",
    "b = 4\n",
    "print('{0}/{1} = {2}'.format(a, b, a/b))\n",
    "print('{0}//{1} = {2}'.format(a, b, a//b))\n",
    "print('{0}%{1} = {2}'.format(a, b, a%b))\n",
    "print(a == b * (a//b) + a%b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "13/-4 = -3.25\n",
      "13//-4 = -4\n",
      "13%-4 = -3\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "a = 13\n",
    "b = -4\n",
    "print('{0}/{1} = {2}'.format(a, b, a/b))\n",
    "print('{0}//{1} = {2}'.format(a, b, a//b))\n",
    "print('{0}%{1} = {2}'.format(a, b, a%b))\n",
    "print(a == b * (a//b) + a%b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-13/-4 = 3.25\n",
      "-13//-4 = 3\n",
      "-13%-4 = -1\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "a = -13\n",
    "b = -4\n",
    "print('{0}/{1} = {2}'.format(a, b, a/b))\n",
    "print('{0}//{1} = {2}'.format(a, b, a//b))\n",
    "print('{0}%{1} = {2}'.format(a, b, a%b))\n",
    "print(a == b * (a//b) + a%b)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
